package com.nwpu.am.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.nwpu.am.mapper.KnowledgeMapper;
import com.nwpu.am.pojo.dto.KnowledgeDTO;
import com.nwpu.am.pojo.dto.page.KnowledgePageDTO;
import com.nwpu.am.pojo.entity.Knowledge;
import com.nwpu.am.result.PageResult;
import com.nwpu.am.service.KnowledgeService;
import com.nwpu.am.utils.id_generator.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * KnowledgeServiceImpl
 *
 * @author Fbf
 * @version 1.0
 * {@code @date} 2024/6/5
 */
@Service
@Slf4j
public class KnowledgeServiceImpl implements KnowledgeService {
    @Resource
    private KnowledgeMapper knowledgeMapper;
    @Resource
    private IdGenerator idGenerator;

    @Override
    public int addKnowledge(KnowledgeDTO knowledgeDTO) {
        Knowledge knowledge = Knowledge.builder()
                .knowledgeId(idGenerator.nextId())
                .type(knowledgeDTO.getType())
                .description(knowledgeDTO.getDescription())
                .build();
        return knowledgeMapper.insertKnowledge(knowledge);
    }

    @Override
    public int deleteKnowledge(long knowledgeId) {
        return knowledgeMapper.deleteKnowledgeById(knowledgeId);
    }

    @Override
    public Knowledge selectById(long knowledgeId) {
        return knowledgeMapper.selectKnowledgeById(knowledgeId);
    }

    @Override
    public int update(Knowledge knowledge) {
        return knowledgeMapper.update(knowledge);
    }

    @Override
    public PageResult<Knowledge> pageQuery(KnowledgePageDTO knowledgePageDTO) {
        // 开启分页插件
        PageMethod.startPage(knowledgePageDTO.getPage(), knowledgePageDTO.getPageSize());

        // 调用分页方法
        Page<Knowledge> page = knowledgeMapper.pageQuery(knowledgePageDTO);

        long total = page.getTotal();
        List<Knowledge> result = page.getResult();

        return new PageResult<>(total, result);
    }
}
